package com.cmccgz.udacs_landlord_webapp.main.host;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;

import com.cmccgz.baselibrary.utils.SharedPreferencesUtil;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.base.GlobalContext;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import java.util.List;

import rx.Subscriber;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/7/12 14:41
 */

public class SyncDeviceIntentService extends IntentService {

    public SyncDeviceIntentService() {
        super("SyncDeviceIntentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        ApiService.deviceSync()
                .subscribe(new Subscriber<ResultBean<List<DeviceInfoBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResultBean<List<DeviceInfoBean>> listResultBean) {
                        List<DeviceInfoBean> resp = listResultBean.resp;
                        Gson gson = new Gson();
                        String json = gson.toJson(resp);
                        json=json==null?"":json;
                        Logger.json(json);
                        SharedPreferencesUtil.putString(GlobalContext.getInstance(), AppConfig.SP_DEVICES_INFO, json);
                    }
                });
    }
}
